﻿using Utils.Wpf;

namespace StockAnalyst2.Options
{
	internal class ApplicationOptionsVM : ViewModelValidatedBase
	{
		#region Properties

		public string UserName
		{
			get 
			{
				return this.RealTimeEnabled ? this.Model.UserName : string.Empty;
			}
			set
			{
				if (this.Model.UserName != value)
				{
					this.Model.UserName = value;
					base.OnPropertyChanged("UserName");
				}
			}
		}

		public string Password
		{
			get 
			{
				return this.RealTimeEnabled ? this.Model.Password : string.Empty;
			}
			set
			{
				if (this.Model.Password != value)
				{
					this.Model.Password = value;
					base.OnPropertyChanged("Password");
				}
			}
		}

		public string DatabaseServer
		{
			get
			{
				return this.Model.DatabaseServer;
			}
			set
			{
				if (this.Model.DatabaseServer != value)
				{
					this.Model.DatabaseServer = value;
					base.OnPropertyChanged("DatabaseServer");
				}
			}
		}

		public string DatabaseName
		{
			get
			{
				return this.Model.DatabaseName;
			}
			set
			{
				if (this.Model.DatabaseName != value)
				{
					this.Model.DatabaseName = value;
					base.OnPropertyChanged("DatabaseName");
				}
			}
		}

		public bool RealTimeEnabled
		{
			get
			{
				return this.Model.RealTimeEnabled;
			}
			set
			{
				if (this.Model.RealTimeEnabled != value)
				{
					this.Model.RealTimeEnabled = value;
					base.OnPropertyChanged("RealTimeEnabled");
					base.OnPropertyChanged("UserName");
					base.OnPropertyChanged("Password");
					base.OnPropertyChanged("ContractNumber");
				}
			}
		}

		public ApplicationOptions Model { get; private set; }

		#endregion

		#region Public Methods

		public ApplicationOptionsVM(ApplicationOptions model)
		{
			this.Model = model;
		}

		protected override string ValidateField(string fieldName)
		{
			string result = null;

			if (fieldName == "UserName")
			{
				if (string.IsNullOrEmpty(this.UserName) && this.RealTimeEnabled)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "Please enter a UserName";
				}
			}
			else if (fieldName == "Password")
			{
				if (string.IsNullOrEmpty(this.Password) && this.RealTimeEnabled)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "Please enter a Password";
				}
			}
			else if (fieldName == "DatabaseServer")
			{
				if (string.IsNullOrEmpty(this.DatabaseServer))
				{
					base.ValidatedErrors[fieldName] = true;
					result = "Please enter a Database Server";
				}
			}
			else if (fieldName == "DatabaseName")
			{
				if (string.IsNullOrEmpty(this.DatabaseName))
				{
					base.ValidatedErrors[fieldName] = true;
					result = "Please enter a Database Name";
				}
			}
			else if (fieldName == "RealTimeEnabled")
			{
				// Do nothing here, no errors can happen on this field.
			}

			return result;
		}

		#endregion
	}
}
